$(document).ready(function () {
    //initDeptSelect();
    initDeptSelect();
    initEdu();
   // initTable();
    initTable2();
    $('#btnEdit').bind('click',btnEditClick);
    $('#btnAdd').bind('click',btnAddClick);
    $('#btnSaveForEdit').bind('click',btnSaveForEditClick);
    $('#btnSaveForAdd').bind('click',btnSaveForAddClick);
    initEdForEdit();
})


function btnSaveForAddClick() {

    $.ajax({
        /*localhost:8088/emp/add*/
        url:'http://localhost:8088/nor/add',
        method:'post',
        dataType:'json',
          data:{
            name:$('#nameForEdit').val(),
            edu:$('#eduForEdit').text(),
            phone:$('#phoneForEdit').val()
          },
        success:function (data) {
            if(data.flag){
                /***
                 * 1.在模块窗体初始化时，隐藏成功信息？
                 * 2. 给成功信息添加适当的样式效果
                 * */
                $('#successMessage').show(2000,function () {
                    initTable2();
                    $('#editInfo').modal('hide');

                })
            }

        }
    })

}

function btnAddClick() {

    $('#btnSaveForAdd').show();
    $('#btnSaveForEdit').hide();
    $('#titleForInfo').text("员工档案-创建");

    initForm();

    $('#editInfo').modal();

}

function initForm() {
    $('#editInfo input').val("");
    $('#eduForEdit').empty();
    initEdForEdit();
}

function btnSaveForEditClick() {

    $.ajax({
    /*    url:'http://test.org/edit',*/
        url:'http://localhost:8088/nor/edit',
        method:'post',
        dataType:'json',
        data:{
          id:$('#empId').val(),
          name:$('#nameForEdit').val(),
          edu:$('#eduForEdit').text(),
          phone:$('#phoneForEdit').val()
        },
        success:function (data) {
            if(data.flag){

                initTable2();

                $('#editInfo').modal('hide');
            }

        }
    })

}

function btnEditClick() {

    $('#btnSaveForAdd').hide();
    $('#btnSaveForEdit').show();
    $('#titleForInfo').text("员工档案-修改");



    var row = $('#list').bootstrapTable('getSelections')[0];
    console.log(row);
    if(row){
        $('#nameForEdit').val(row.name);
        $('#eduForEdit').find('option:contains('+row.edu+')').attr('selected',true);
        $('#phoneForEdit').val(row.phone);
        $('#empId').val(row.id);

        $('#editInfo').modal();
    }else {
         alert("请选择你需要编辑的数据！")
    }

}



function initEdForEdit() {
    $.ajax({
     /*   url:'http://localhost:8088/nor/edu',*/
        url:'http://test.org/emp/edu/list',

        method:'get',
        dataType:'json',
        success:function (data) {
            console.log(data);
            var opt_all = $('<option></option>').val(0).text("-----所有学历------");
            $('#eduForEdit').append(opt_all);
            $.each(data,function (index,item) {
                var opt = $('<option></option>').val(item.id).text(item.name);
                $('#eduForEdit').append(opt);
            })
        }
    })
}



function initTable2() {
    $('#list').bootstrapTable('destroy');

    $('#list').bootstrapTable({
        url:'http://localhost:8088/nor/list',
        columns:columns2,
        pagination:true,
        pageSize:5,
        sidePagination:"client",
        singleSelect:true,
        clickToSelect:true,
        toolbar:'#toolbar'
    })
}

var columns2 =[
    {
        checkbox:true
    },
    {
        field:'name',
        title:'姓名'
    },{
        field:"code",
        title:"员工编号"
    },
   {
        field:"edu",
        title:"学历"
    },{
        field:"gender",
        title:"性别"
    },{
        field:"phone",
        title:"电话"
    },{
        field:"id",
        title:"操作",
        formatter:function (val,row,index) {
            var container = $('<div></div>');
            var btnEdit = $('<button></button>').text("修改")
                .attr('onclick','return btnEditClick()')

                .addClass('btn').addClass('btn-warning');
            container.append(btnEdit);
            return container.html();
        }
    }
]


function initDeptSelect2() {
    $.ajax({
        url:'http://localhost:8088/nor/dept',
        method:'get',
        dataType:'json',
        success:function (data) {
            console.log(data);
            var opt_all = $('<option></option>').val(0).text("-----所有部门-------");
            $('#deptForQuery').append(opt_all);
            $.each(data,function (index,item) {
                var opt = $('<option></option>').val(item.deptId).text(item.deptName)
                $('#deptForQuery').append(opt);
            })
        }
    })
}
function initTable() {

    $('#list').bootstrapTable('destroy');
    $('#list').bootstrapTable({
        url:'http://test.org/emp/nor/list',
        columns:columns,
        pagination:true,
        pageSize:5,
        sidePagination:"client",
        singleSelect:true,
        clickToSelect:true,
        toolbar:'#toolbar'
    })
}


var columns =[
    {
        checkbox:true
    },
    {
        field:'name',
        title:'姓名'
    },{
        field:"empNO",
        title:"员工编号"
    },
    {
        field:"dept.name",
        title:"部门"
    },{
        field:"edu.name",
        title:"学历"
    },{
        field:"gender",
        title:"性别"
    },{
        field:"age",
        title:"年龄"
    },{
        field:"id",
        title:"操作",
        formatter:function (val,row,index) {
            var container = $('<div></div>');
            var btnEdit = $('<button></button>').text("修改")
                .attr('onclick','return btnEditClick()')
                .addClass('btn').addClass('btn-warning');

            var btnRemove = $('<button></button>').text("删除")
                .attr('onclick','return btnRemoveClick()')
                .addClass('btn').addClass('btn-danger');


            container.append(btnEdit);

            container.append(btnRemove);



            return container.html();
        }
    }
]


function btnRemoveClick() {

    var row = $('#list').bootstrapTable('getSelections')[0];
    if(row){
        if(confirm("是否删除当前数据")){
              $.ajax({
                  /** localhost:8088/nor/remove/*/
                  url:'http://test.org/remove',
                  method:'get',
                  dataType:'json',
                  success:function (data) {
                      if(data.flag){
                          alert("数据删除成功！")
                          // 重新加载数据
                          initTable();
                      }
                  }
              })
        }
    }else {
        alert("请选择您要操作的数据!")
    }


}

function initDeptSelect() {
    $.ajax({
        url:'http://test.org/emp/dept/list',
        method:'get',
        dataType:'json',
        success:function (data) {
             console.log(data);
            var opt_all = $('<option></option>').val(0).text("-----所有部门-------");
            $('#deptForQuery').append(opt_all);
            $.each(data,function (index,item) {
                var opt = $('<option></option>').val(item.deptId).text(item.name)
                $('#deptForQuery').append(opt);
            })
        }
    })
}



function initEdu() {
    $.ajax({
        url:'http://localhost:8088/nor/edu',
        method:'get',
        dataType:'json',
        success:function (data) {
            console.log(data);
             var opt_all = $('<option></option>').val(0).text("-----所有学历------");
            $('#eduForQuery').append(opt_all);
            $.each(data,function (index,item) {
                var opt = $('<option></option>').val(item.eduId).text(item.title);
                $('#eduForQuery').append(opt);
            })
        }

    })

}
